/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Dawn
 */
@WebServlet(name="Admin", urlPatterns={"/Admin"})
public class Admin extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        ResultSet rst=null;
        Statement stm=null;

        try {
            
            
            
            Connection connection = null;

            try{
                try {
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                } catch (InstantiationException ex) {
                    Logger.getLogger(LoginProcess.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(LoginProcess.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            catch (ClassNotFoundException ex){
            }

            connection = DriverManager.getConnection("jdbc:mysql://localhost/chatter?user=root&password=");

            

            

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Admin</title>");
            out.println("<link rel='stylesheet' href='CSS/MenuStyle.css' type='text/css' />");
            out.println("</head>");
            out.println("<body>");

            out.println("<div id='sidebar'>");
            out.println("<h1>Chit-Chat</h1>");
            out.println("<h2>Tubes 4 Progin</h2>");
            out.println("<p>");
            out.println("Created by :<br /><br />");
            out.println("Archie Anugrah - 13508001<br />");
            out.println("Darwin Soesanto - 13508102<br />");
            out.println("Listiarso Wastuargo - 13508103<br />");
            out.println("</p>");
            out.println("<div id='menu'>");
            out.println("<a class='active' href='#'>Admin</a>");
            out.println("<a href='Logout.jsp'>Logout</a>");
            out.println("</div>");
            out.println("</div>");


            out.println("<div id='content'>");
            out.println("<label>Users</label>");
            out.println("<table>");
            out.println("<tr>");
            out.println("<td>Nama</td>");
            out.println("<td>Username</td>");
            out.println("<td>Password</td>");
            out.println("<td>Email</td>");
            out.println("<td>MaxRoom</td>");
            out.println("<td>Role</td>");
            out.println("<td>Banned</td>");
            out.println("</tr>");

            stm=connection.createStatement();
            if (stm.execute("select * from users"))
            {
                rst=stm.getResultSet();
                while (rst.next())
                {
                    out.println("<tr>");
                    out.println("<td>" + rst.getString(1) + "</td>");
                    out.println("<td>" + rst.getString(2) + "</td>");
                    out.println("<td>" + rst.getString(3) + "</td>");
                    out.println("<td>" + rst.getString(4) + "</td>");
                    out.println("<td>" + rst.getString(5) + "</td>");
                    out.println("<td>" + rst.getString(6) + "</td>");
                    out.println("<td>" + rst.getString(7) + "</td>");
                    out.println("<td><a href=Privi?Username=" + rst.getString(2) + "&Pri=" + rst.getString(6) + ">Change Priviledge</a></td> <td><a href=Ban?Username=" + rst.getString(2) + "&B=" + rst.getString(7) + ">Ban User</a></td>");

                    out.println("<td>");

                    out.println("<form action='maxRoom' id='maxRoomchange'>");
                    out.println("<input type='text' size=2 value=" + rst.getString(5) + " name='roomMax'>");
                    out.println("<input type='hidden' size=2 value=" + rst.getString(2) + " name='Username'>");
                    out.println("<input type='submit' value='set RoomMax' name='submitroomMax'>");
                    out.println("</form>");

                    out.println("</td>");
                    out.println("</tr>");
                }
            }

            out.println("</table>");

            out.println("</div>");

            out.println("<div id='content'>");
            out.println("<label>Category</label>");
            out.println("<table>");
            out.println("<tr>");
            out.println("<td>ID</td>");
            out.println("<td>Parent_ID</td>");
            out.println("<td>Name</td>");
            out.println("</tr>");

            stm=connection.createStatement();
            if (stm.execute("select * from category"))
            {
                rst=stm.getResultSet();
                while (rst.next())
                {
                    out.println("<tr>");
                    out.println("<td>" + rst.getString(1) + "</td>");
                    out.println("<td>" + rst.getString(2) + "</td>");
                    out.println("<td>" + rst.getString(3) + "</td>");
                    out.println("<td><a href=DelCate?ID=" + rst.getString(1) + ">Delete Category</a></td>");
                    out.println("<td>");

                    out.println("<form action='EditCate?ID=" + rst.getString(1) + "' id='EditCate'>");
                    out.println("<input type='text' size=2 value=" + rst.getString(2) + " name='CateParent'>");
                    out.println("<input type='text' size=8 value=" + rst.getString(3) + " name='CateName'>");
                    out.println("<input type='hidden' size=2 value=" + rst.getString(1) + " name='CateID'>");
                    out.println("<input type='submit' value='Change' name='EditCategory'>");
                    out.println("</form>");

                    out.println("</td>");
                    out.println("</tr>");
                }
            }

            out.println("</table>");

            out.println("</div>");

            out.println("<div id='content'>");
            out.println("<label>Add Room</label><p/>");
            out.println("To get a stand alone category, leave it empty");
            
            out.println("<table>");
            out.println("<tr>");
            out.println("<form action='AddCate' id='addCate'>");
            out.println("<tr><td>Category Name : </td><td><input type='text' size=10 name='CateName'></td></tr>");
            out.println("<tr><td>Category Parent : </td><td><input type='text' size=2 name='CateParent'></td></tr>");
            out.println("<input type='submit' value='Add Category' name='submitAddCate'>");
            out.println("</form>");
            out.println("</tr>");
            out.println("</table>");

            out.println("</div>");

            out.println("</body>");
            out.println("</html>");
            
        } finally {
            rst.close();
            stm.close();
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(Admin.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
